package com.zy.explore.order.domain.jpa.entity;

import com.zy.explore.order.domain.jpa.repository.OrderRepository;
import com.zy.explore.order.interfaces.frm.OrderFrm;
import com.zy.explore.common.infrastructure.base.jpa.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.criteria.Predicate;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (表t_order)ORM实体类
 *
 * @author 中国印
 * @date 2021-09-20 20:25:36
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicUpdate
@DynamicInsert
@Table(name = "t_order")
public class Order extends BaseEntity<OrderRepository, Order, Integer> {


    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    @Column(name = "id", insertable = false, updatable = false, nullable = false, columnDefinition = "bigint(20) COMMENT '表主键'")
    private Integer id;

    /**
     * 产品ID
     */
    @Column(name = "goods_id")
    private Integer goodsId;

    /**
     * 订单金额
     */
    @Column(name = "amount")
    private BigDecimal amount;

    /**
     * 买家ID
     */
    @Column(name = "buyer_id")
    private Integer buyerId;

    /**
     * 卖家ID
     */
    @Column(name = "seller_id")
    private Integer sellerId;

    /**
     * 订单状态：1.待付款 2.已付款 3.已完成
     */
    @Column(name = "order_status")
    private String orderStatus;

    /**
     * 付款时间
     */
    @Column(name = "pay_time")
    private Date payTime;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    private static OrderRepository repository() {
        return Order.Holder.REPO;
    }

    private static final class Holder {
        private static final OrderRepository REPO = getRepository(OrderRepository.class, Order.class);
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<Order> getSpecification(OrderFrm frm) {
        if (frm.getPageNum() == null) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (frm.getId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (frm.getGoodsId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("goodsId"), frm.getGoodsId()));
            }
            if (frm.getAmount() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("amount"), frm.getAmount()));
            }
            if (frm.getBuyerId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("buyerId"), frm.getBuyerId()));
            }
            if (frm.getSellerId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("sellerId"), frm.getSellerId()));
            }
            if (!StringUtils.isEmpty(frm.getOrderStatus())) {
                predicateList.add(criteriaBuilder.like(root.get("orderStatus"), "%" + frm.getOrderStatus() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getPayTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("payTime").as(String.class), frm.getPayTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getPayTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("payTime").as(String.class), frm.getPayTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

}
